package com.ecloud.common.base.result;

import java.io.UnsupportedEncodingException;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import com.ecloud.common.base.exception.BusinessException;

/**
 * 国际化资源文件操作类
 * 
 * @author LiuGangQiang Create in 2020/03/01
 */
public final class Prompt {
    /**
     * 默认资源文件路径 默认值为 {@value}
     *
     * @author LiuGangQiang Create in 2020/03/01
     */
    private final static String FILE_PATH = "i18n.message";

    /**
     * 构造器私有化
     *
     * @author LiuGangQiang Create in 2020/03/01
     */
    private Prompt() {
    }

    /**
     * 获取无占位符的属性值
     *
     * @author LiuGangQiang Create in 2020/03/01
     * @param key 键值
     * @return {@link String}
     */
    public static String bundle(String key) {
        return getMessage(Locale.getDefault(), FILE_PATH, key);
    }

    /**
     * 获取无占位符的属性值
     *
     * @author LiuGangQiang Create in 2020/03/30
     * @param locale 语言环境
     * @param key    键值
     * @return {@link String}
     */
    public static String bundle(Locale locale, String key) {
        return getMessage(locale, FILE_PATH, key);
    }

    /**
     * 获取有占位符的属性值并赋值
     *
     * @author LiuGangQiang Create in 2020/03/01
     * @param key       键值
     * @param arguments 参数（数组或不定参数）
     * @return {@link String}
     */
    public static String bundle(String key, Object... arguments) {
        return getMessage(Locale.getDefault(), FILE_PATH, key, arguments);
    }

    /**
     * 获取有占位符的属性值并赋值
     *
     * @author LiuGangQiang Create in 2020/03/01
     * @param locale    语言环境
     * @param key       键值
     * @param arguments 参数（数组或不定参数）
     * @return {@link String}
     */
    public static String bundle(Locale locale, String key, Object... arguments) {
        return getMessage(locale, FILE_PATH, key, arguments);
    }

    /**
     * 获取无占位符的属性值
     *
     * @author LiuGangQiang Create in 2020/03/01
     * @param path 文件路径
     * @param key  键值
     * @return {@link String}
     */
    public static String bundle(String path, String key) {
        return getMessage(Locale.getDefault(), path, key);
    }

    /**
     * 获取无占位符的属性值
     *
     * @author LiuGangQiang Create in 2020/03/01
     * @param locale 语言环境
     * @param path   文件路径
     * @param key    键值
     * @return {@link String}
     */
    public static String bundle(String path, Locale locale, String key) {
        return getMessage(locale, path, key);
    }

    /**
     * 获取有占位符的属性值并赋值
     *
     * @author LiuGangQiang Create in 2020/03/01
     * @param path      文件路径
     * @param key       键值
     * @param arguments 参数（数组或不定参数）
     * @return {@link String}
     */
    public static String bundle(String path, String key, Object... arguments) {
        return getMessage(Locale.getDefault(), path, key, arguments);
    }

    /**
     * 获取有占位符的属性值并赋值
     *
     * @author LiuGangQiang Create in 2020/03/01
     * @param path      文件路径
     * @param key       键值
     * @param locale    语言环境
     * @param arguments 参数（数组或不定参数）
     * @return {@link String}
     */
    public static String bundle(String path, Locale locale, String key, Object... arguments) {
        return getMessage(locale, path, key, arguments);
    }

    /**
     * 获取消息
     *
     * @author LiuGangQiang Create in 2020/04/02
     * @param locale    语音环境
     * @param path      文件路径
     * @param key       键值
     * @param arguments 参数
     * @return {@link String}
     */
    private static String getMessage(Locale locale, String path, String key, Object... arguments) {
        ResourceBundle bundle = ResourceBundle.getBundle(path, locale);
        try {
            String value = new String(bundle.getString(key).getBytes("ISO-8859-1"), "UTF-8");
            if (arguments != null && arguments.length > 0) {
                return MessageFormat.format(value, arguments);
            } else {
                return value;
            }
        } catch (UnsupportedEncodingException e) {
            throw new BusinessException("resource bundle get bundle message error path={0} locale={1} key={2} args={3}", path, locale, key, arguments);
        }
    }

    /**
     * @return the filepath
     */
    protected static String getFilepath() {
        return FILE_PATH;
    }

}
